package cn.com.allunion.sms.service.impl;

import cn.com.allunion.sms.service.SMSOperatorService;
import com.cn.dao.util.HibernateDAO;
import com.cn.dao.util.Page;
import com.cn.entity.SMSOperator;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 业务逻辑实现类
 * Created by YangJie on 2016/4/13.
 */
@Service
public class SMSOperatorServiceImpl implements SMSOperatorService {
    private static final Logger LOGGER = LoggerFactory.getLogger(SMSOperatorServiceImpl.class) ;

    @Value("${pagesize}")
    int pagesize;

    @Autowired
    HibernateDAO hibernateDao;

    /**
     * 添加
     *
     * @param smsOperatorName
     *            运营商名称
     * @param priority
     *            运营商优先级
     * @param joinNum
     *            接入号
     * @param joinState
     *            接入状态
     * @return 方法执行状态
     */
    @Transactional(rollbackForClassName = "Exception")
    public String addNewOperator(String smsOperatorName, int priority, String joinNum, String joinState) {
        SMSOperator smsOperator = new SMSOperator();
        smsOperator.setSmsOperatorName(smsOperatorName);
        smsOperator.setPriority(priority);
        smsOperator.setJoinNum(joinNum);
        smsOperator.setJoinState(joinState);
        return this.hibernateDao.saveOrUpdate(smsOperator);
    }

    /**
     * 根据id修改运营商
     *
     * @param id
     *            运营商ID
     * @param smsOperatorName
     *            运营商名称
     * @param priority
     *            运营商优先级
     * @param joinNum
     *            接入号
     * @param joinState
     *            接入状态
     * @return 方法执行状态
     */
    @Transactional(rollbackForClassName = "Exception")
    public String udpateOperator(String id, String smsOperatorName,
                                 int priority, String joinNum, String joinState) {
        // if(id==null||id.equals("")){
        // SMSOperatorAction action = new SMSOperatorAction();
        // return action.addNewOperator(smsOperatorName, priority, joinNum,
        // joinState);
        // }else{
        SMSOperator smsOperator = hibernateDao.listById(SMSOperator.class, id,
                true);
        if (smsOperator != null) {

            smsOperator.setSmsOperatorName(smsOperatorName);
            smsOperator.setPriority(priority);
            smsOperator.setJoinNum(joinNum);
            smsOperator.setUpdateDate(new Date());
            smsOperator.setJoinState(joinState);
            return hibernateDao.saveOrUpdate(smsOperator);
        }
        return "error";
        // }
    }

    /**
     * 无条件查询全部内容
     *
     * @return 运营商列表
     */
    @Transactional(readOnly = true)
    public List<SMSOperator> findByOperatorId() {

        return hibernateDao.listByAll(SMSOperator.class, true);

    }

    /**
     * 无条件查询全部内容
     *
     * @param pageCurrent
     *            当前页号
     * @return 运营商分页列表
     */
    @Transactional(readOnly = true)
    public Page<SMSOperator> findByOperatorId(int pageCurrent) {

        int allcount = hibernateDao.count(SMSOperator.class, false);

        int lastPage = lastPage(allcount, pagesize);
        if (lastPage < pageCurrent) {
            pageCurrent = lastPage;
        }
        if (lastPage <= 1) {
            pageCurrent = 0;
        }
        // List<SMSOperator> list = hibernateDao.listByAll(SMSOperator.class,
        // true);
        List<SMSOperator> list = hibernateDao.listByPage(SMSOperator.class,
                pagesize, pageCurrent, true);
        Page<SMSOperator> page = new Page<SMSOperator>();
        page.setList(list);
        page.setTotal(allcount);
        page.setPageSize(pagesize);
        page.setPageTotal(lastPage);
        return page;
    }

    /**
     * 相对位置
     *
     * @param count
     *            总条数
     * @param pageSize
     *            每页显示页数
     * @return 相对位置
     */
    public int lastPage(int count, int pageSize) {
        if (count % pageSize == 0) {
            return count / pageSize;
        }

        return count / pageSize + 1;
    }

    public int getPagesize() {
        return pagesize;
    }

    public void setPagesize(int pagesize) {
        this.pagesize = pagesize;
    }

    /**
     * 根据smsOperatorName 查找，返回对象
     *
     * @param smsOperatorName
     * @return 运营商
     */
    @Transactional(readOnly = true)
    public SMSOperator findOperatorObj(String smsOperatorName) {

        LOGGER.debug("*********************" + smsOperatorName);

        List<SMSOperator> operatrList = new ArrayList();

        operatrList = hibernateDao.listByCriteria(SMSOperator.class,
                Restrictions.eq("joinNum", smsOperatorName), true);

        if (operatrList.size() != 0) {
            LOGGER.debug("*********************" + operatrList.get(0).getSmsOperatorName());
            return operatrList.get(0);
        } else {
            LOGGER.debug("*********************" + operatrList.size());
            return null;
        }

    }

    /**
     * 取得运营商实例
     *
     * @param smsOperatorName
     *            运营商名字
     * @return 运营商
     */
    public SMSOperator shenma(String smsOperatorName) {

        LOGGER.debug("*********************" + smsOperatorName);

        List<SMSOperator> operatrLists = new ArrayList();

        // String sqls = "from SMSOperator where joinNum='"+smsOperatorName+"'";

        operatrLists = hibernateDao.listByCriteria(SMSOperator.class,
                Restrictions.eq("joinNum", smsOperatorName), true);

        if (operatrLists.size() != 0) {

            LOGGER.debug("*********************" + operatrLists.get(0).getSmsOperatorName());
            return operatrLists.get(0);
        } else {

            LOGGER.debug("*********************" + operatrLists.size());
            return null;
        }

    }

    /**
     * 通过运营商名称查找
     *
     * @param opername
     * @return 运营商
     */
    @Transactional(readOnly = true)
    public SMSOperator findbyName(String opername) {
        SMSOperator operator = null;
        List<SMSOperator> operators = this.hibernateDao.listByCriteria(
                SMSOperator.class,
                Restrictions.eq("smsOperatorName", opername), true);
        if (operators.size() != 0) {
            operator = operators.get(0);
        }
        return operator;
    }
}
